//
//  WZGShareOrderPayVC.m
//  weizhegou
//
//  Created by sujeking on 2020/3/28.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "WZGShareOrderPayVC.h"
#import "WZGShareOrderPayGoodsCell.h"
#import "PaymentManager.h"
#import <BlocksKit+UIKit.h>
#import <Masonry.h>
#import "ZCOrderListVC.h"
@interface WZGShareOrderPayVC ()<WXApiManagerDelegate>

@property (nonatomic, strong) UILabel *priceLabel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSArray *datasource;

@end

@implementation WZGShareOrderPayVC

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setupUI];
}

- (void)setupUI {
  self.navTitle = @"发起代付请求";
  self.tableView.dataSource = self;
  self.tableView.delegate = self;
  [self.tableView registerNib:[UINib nibWithNibName:@"WZGShareOrderPayGoodsCell" bundle:nil]
       forCellReuseIdentifier:@"WZGShareOrderPayGoodsCell"];
  
  
  UILabel *titlelabel = [UILabel new];
  self.priceLabel = [UILabel new];
  UIButton *doneBtn = [UIButton new];
  UILabel *tiplabel = [UILabel new];
  
  self.tableView.layer.cornerRadius = 5.0;
  self.tableView.layer.masksToBounds = YES;
  self.tableView.showsVerticalScrollIndicator = NO;
  
  UIView *view = [[UIView alloc] initWithFrame:(CGRect){0,0,ScreenWidth,290/375.0 * ScreenWidth}];
  
         
         
         
          [view addSubview:titlelabel];
         [view addSubview:self.priceLabel];
         [view addSubview:doneBtn];
         [view addSubview:tiplabel];
         self.tableView.tableHeaderView = view;
         
         
  titlelabel.text = @"通过微信将代付请求发送好友，即可让他帮你买单！";
  titlelabel.font = [UIFont systemFontOfSize:14];
  tiplabel.numberOfLines = 0;
  tiplabel.lineBreakMode = NSLineBreakByWordWrapping;
  titlelabel.textColor = [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"];
  [titlelabel  mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(12);
    make.top.mas_equalTo(40);
    make.width.mas_equalTo(view.mas_width).offset(24);

  }];
         
  self.priceLabel.font = [UIFont systemFontOfSize:35];
  self.priceLabel.textColor = [UIColor redColor];
  self.priceLabel.text = [NSString stringWithFormat:@"￥%@",[self.dict valueForKey:@"actual_fee"]];
  
  self.priceLabel.textAlignment = NSTextAlignmentCenter;
  [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(12);
    make.top.mas_equalTo(titlelabel.mas_bottom).offset(15);
    make.right.mas_equalTo(-12);
  }];
         
  doneBtn.backgroundColor = [UIColor redColor];
  [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  doneBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  [doneBtn setTitle:@"发送代付请求" forState: UIControlStateNormal];
  [doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(12);
    make.top.mas_equalTo(self.priceLabel.mas_bottom).offset(15);
    make.right.mas_equalTo(-12);
    make.height.mas_equalTo(44);;
  }];
  
  doneBtn.layer.cornerRadius = 3.0;
  doneBtn.layer.masksToBounds = YES;
         
  [doneBtn bk_addEventHandler:^(id sender) {
    [self share2Weixin];
  } forControlEvents:UIControlEventTouchUpInside];

  tiplabel.textColor = [generalToolBox getColorWithHexadecimalColorStr:@"0x999999"];
  tiplabel.font = [UIFont systemFontOfSize:13];
  tiplabel.numberOfLines = 0;
  tiplabel.text = [NSString stringWithFormat:@"温馨提示：\n\n1.对方需要开通微信支付才能帮你付款，如果未开通，请重新选择好友发送；\n\n2.如果发起退款，已支付金额将原路退回付款好友。"];
  [tiplabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(12);
    make.top.mas_equalTo(doneBtn.mas_bottom).offset(15);
    make.right.mas_equalTo(-12);

  }];
  
  self.datasource = [self.dict valueForKey:@"orderExtend"];
}

// MARK: - Action

-(void)share2Weixin {
  NSArray *orderExtends = [self.dict valueForKey:@"orderExtends"];
  NSDictionary *orderExtend = orderExtends.firstObject;
  PaymentManager *manager = [PaymentManager sharedManager];
  manager.delegate = self;
  NSString *actual_fee = [self.dict valueForKey:@"actual_fee"];
  NSString *mainstr = [NSString stringWithFormat:@"%@希望你帮TA付%@元",self.userModel.member.username,actual_fee];
  
  NSString *substr = @"我在微折购上买到了很赞的东西，希望你帮我付款哦~";
  NSNumber *idnum = [self.dict valueForKey:@"id"];
  extern NSString *baseURLStr;
  NSString *urlstr = [NSString stringWithFormat:@"%@/h5/index.html#/payForAnotherFriend?order_id=%@&uid=%@",baseURLStr,idnum,self.userModel.member.idstr];
  [manager share2WeiXinFirendList:@[mainstr,substr,[orderExtend valueForKey:@"goods_thumb"],urlstr]];
}

// MARK: - WXApiManagerDelegate

- (void)managerDidRecvShareResponse:(SendMessageToWXResp *)response {
  if (response.errCode == 0 ){
    ZCOrderListVC *vc = [[ZCOrderListVC alloc] init];
    NSMutableArray *vcs = [[self.navigationController childViewControllers] mutableCopy];
    [vcs removeObject:self];
    [vcs addObject:vc];
    [self.navigationController setViewControllers:vcs animated:YES];
  } else {
    [self.view makeToast:response.errStr duration:2.0 position:CSToastPositionCenter];
  }
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.datasource.count;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSInteger row = indexPath.row;
  WZGShareOrderPayGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WZGShareOrderPayGoodsCell"
                                                                    forIndexPath:indexPath];
  
  return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  return 110.0f;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  return 90.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 15.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
}

@end
